/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * cadastroRuas.java
 *
 * Created on 29/09/2009, 21:32:30
 */

package cparc;

import com.mysql.jdbc.Statement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Administrador
 */
public class cadastroRuas extends javax.swing.JInternalFrame {
   boolean alterar = false;
   boolean cadastrar = false;

    static Home frameHome;

    public void setHome(Home home){

        frameHome = home;
    }

    public void bairro(String bCod, String bNom){

            tfCodBai.setText(bCod);
            tfNomBai.setText(bNom);
            tfCodBai.requestFocusInWindow();
            tfCodBai.selectAll();
    }

    public void escPesqBairro(){

            tfCodBai.requestFocusInWindow();
            tfCodBai.selectAll();
    }
    /** Creates new form cadastroRuas */
    public cadastroRuas() {
        initComponents();

        tfCodRua.disable();
        tfNomRua.disable();
        tfCodBai.disable();
        tfNomBai.disable();

       DefaultTableModel modelo = (DefaultTableModel) tbRuas.getModel();
        String search = "select bcod,rcod,rnom,bnom from cparc.cad_vw_bairuas order by rcod" ;
        Statement stm = null;
        int i =0;
            System.out.print(search);
        try  {
                stm = (Statement) conexao.getConnection(true).createStatement();
                ResultSet rs = stm.executeQuery(search);
                rs.afterLast();
                while(rs.previous()){
                    modelo.addRow(new String[] {Integer.toString(rs.getInt("rcod")), rs.getString("rnom"),rs.getString("bcod"),rs.getString("bnom")});
                    modelo.fireTableCellUpdated(i, 0);
                    modelo.fireTableCellUpdated(i, 1);
                    modelo.fireTableCellUpdated(i, 2);
                    modelo.fireTableCellUpdated(i, 3);
                    i++;
                    tfConsulta.requestFocusInWindow();
                    tfConsulta.selectAll();
                }

        }catch(Exception e){
               e.printStackTrace();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        altRuas = new javax.swing.JButton();
        salRuas = new javax.swing.JButton();
        excRuas = new javax.swing.JButton();
        cadRuas = new javax.swing.JButton();
        btConsultar = new javax.swing.JButton();
        tfConsulta = new java.awt.TextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbRuas = new javax.swing.JTable();
        label2 = new java.awt.Label();
        tfNomBai = new java.awt.TextField();
        label1 = new java.awt.Label();
        jSeparator1 = new javax.swing.JSeparator();
        tfCodRua = new java.awt.TextField();
        tfCodBai = new java.awt.TextField();
        label3 = new java.awt.Label();
        label4 = new java.awt.Label();
        tfNomRua = new java.awt.TextField();

        setClosable(true);
        setTitle("Cadastro de Ruas");

        altRuas.setText("Alterar");
        altRuas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                altRuasActionPerformed(evt);
            }
        });

        salRuas.setText("Salvar");
        salRuas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                salRuasActionPerformed(evt);
            }
        });

        excRuas.setText("Excluir");
        excRuas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                excRuasActionPerformed(evt);
            }
        });

        cadRuas.setText("Cadastrar");
        cadRuas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cadRuasActionPerformed(evt);
            }
        });

        btConsultar.setText("Consultar");
        btConsultar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btConsultarActionPerformed(evt);
            }
        });

        tfConsulta.setText("Digite o Nome da Rua");
        tfConsulta.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfConsultaKeyPressed(evt);
            }
        });

        tbRuas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código da Rua", "Nome da Rua", "Cod. Bairro", "Nome do Bairro"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, true, true, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbRuas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbRuasMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbRuas);

        label2.setText("Rua");

        label1.setText("Código da Rua");

        tfCodBai.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfCodBaiKeyPressed(evt);
            }
        });

        label3.setText("Código do Bairro");

        label4.setText(" Bairro");

        tfNomRua.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfNomRuaKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 419, Short.MAX_VALUE)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 419, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(84, 84, 84)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(salRuas, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(altRuas)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cadRuas)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(excRuas)))
                        .addGap(42, 42, 42))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(20, 20, 20)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tfCodRua, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(tfCodBai, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(label4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tfNomBai, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(tfNomRua, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addComponent(tfConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btConsultar, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
                        .addGap(8, 8, 8)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(tfCodRua, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfNomRua, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfCodBai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(label4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfNomBai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 56, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(altRuas)
                    .addComponent(cadRuas)
                    .addComponent(excRuas))
                .addGap(18, 18, 18)
                .addComponent(salRuas)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(tfConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btConsultar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(46, 46, 46))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void altRuasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_altRuasActionPerformed
       tfNomRua.enable();
       tfCodRua.disable();
       tfCodBai.enable();
       tfNomBai.disable();
        if (tfCodRua.getText().equals("") && tfNomRua.getText().equals("")){
        tfConsulta.setText("Digite o nome da Rua para consultar...");
        tfConsulta.requestFocusInWindow();
        tfConsulta.selectAll();
       }else{
           tfNomRua.enable();
           tfCodBai.enable();
           tfNomRua.requestFocusInWindow();
           tfNomRua.selectAll();
           this.repaint();
       }
        alterar = true;
        cadastrar = false;
}//GEN-LAST:event_altRuasActionPerformed

    private void salRuasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salRuasActionPerformed
        if(cadastrar){
        String inserir = "insert into cparc.cad_ruas (rcod,rnom,rbai) values(" + "'" + tfCodRua.getText() +"'" + "," + "'" + tfNomRua.getText()+ "'" + "," + "'" + tfCodBai.getText() + "'" + " )" ;
        System.out.println(tfCodRua.getText());
        System.out.println(tfNomRua.getText());
        System.out.println(tfCodBai.getText());
          try  {
            Statement stm = (Statement) conexao.getConnection(true).createStatement();
             stm.executeUpdate(inserir);

        }
        catch(Exception e){
            e.printStackTrace();
            String status = e.getMessage();
            System.out.println(status);
        }
      }
        if (alterar){
            Statement stm = null;
            String cadastrar = "update cparc.cad_ruas set rnom = '" + tfNomRua.getText() + "', rbai =  " + tfCodBai.getText() + " where rcod = " + tfCodRua.getText() + " ";


            try  {
            stm = (Statement) conexao.getConnection(true).createStatement();
            stm.executeUpdate(cadastrar);
            }
            catch(Exception e){
                String status = e.getMessage();
                System.out.println(status);
            }
        }
            tfNomRua.disable();
            tfCodBai.disable();
            tfConsulta.setText("");
            btConsultar.doClick();
            alterar = false;
            cadastrar = false;
            tfCodRua.setText("");
            tfNomRua.setText("");
            tfCodBai.setText("");
            tfNomBai.setText("");
}//GEN-LAST:event_salRuasActionPerformed

    private void excRuasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_excRuasActionPerformed
          Statement stm = null;
        String excluir = "delete from cparc.cad_ruas where rcod = " + tfCodRua.getText();
        int n;

       Object[] options = { "Confirmar", "Cancelar" };
       n = JOptionPane.showOptionDialog(null, "Você está prestes a excluir a rua: " + tfNomRua.getText() + "!\n Realizar a exclusão?", "Operação de exclusão!", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
       System.out.println(String.valueOf(n));

       if(n == 0)
       {
       try  {
        stm = (Statement) conexao.getConnection(true).createStatement();
        stm.executeUpdate(excluir);
        }
        catch(Exception e){
            String status = e.getMessage();
            System.out.println(status);
        }
       }
       tfConsulta.setText("");
       btConsultar.doClick();
       tfCodRua.setText("");
       tfNomRua.setText("");
}//GEN-LAST:event_excRuasActionPerformed

    private void cadRuasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cadRuasActionPerformed
        tfCodRua.disable();
        tfNomRua.enable();
        tfCodBai.enable();
        tfNomRua.enable();
        tfNomBai.setText("");
        String search = "select rcod from cparc.cad_ruas" ;
        Statement stm = null;
        int codb = 0;
            System.out.print(search);
        try  {
                stm = (Statement) conexao.getConnection(true).createStatement();
                ResultSet rs = stm.executeQuery(search);
                while(rs.next()){
            int cod = Integer.parseInt(rs.getString("rcod"));
            if (cod > codb)
                codb = cod;
        }
        }catch(Exception e){
               e.printStackTrace();
        }
        tfCodRua.setText(String.valueOf(codb + 1));
        tfNomRua.setText("");
        tfNomRua.requestFocusInWindow();
        cadastrar = true;
        alterar = false;
}//GEN-LAST:event_cadRuasActionPerformed

    private void tbRuasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbRuasMouseClicked
       int line = tbRuas.getSelectedRow();
        String cod = (String) tbRuas.getValueAt(line, 0);
        tfCodRua.setText(cod);
        String nom = (String) tbRuas.getValueAt(line, 1);
        tfNomRua.setText(nom);
        String codBai = (String) tbRuas.getValueAt(line,2);
        tfCodBai.setText(codBai);
        String nomBai = (String) tbRuas.getValueAt(line,3);
        tfNomBai.setText(nomBai);
        if(alterar)
        {
        tfNomRua.enable();
        tfNomRua.requestFocusInWindow();
        tfNomRua.selectAll();
        }
    }//GEN-LAST:event_tbRuasMouseClicked

    private void tfCodBaiKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfCodBaiKeyPressed
         int n;
        if(evt.getKeyCode() == 10 && !tfCodBai.getText().isEmpty()){
            salRuas.doClick();
        }else if(evt.getKeyCode() == 10 && tfCodBai.getText().isEmpty()) {
               Object[] options = { "OK" };
               n = JOptionPane.showOptionDialog(null, "Por favor selecione  um bairro, pressionando F3 !", "Alerta Bairro!", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
               System.out.println(String.valueOf(n));

        }else if(evt.getKeyCode() == 114){

             pesqBairro bairro = new pesqBairro();
             bairro.setPropRua(this);
             Home.desk.add(bairro);
             bairro.setVisible(true);
         }
    }//GEN-LAST:event_tfCodBaiKeyPressed

    private void tfConsultaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfConsultaKeyPressed
        if(evt.getKeyCode() == 10 ){
           btConsultar.doClick();
       }
    }//GEN-LAST:event_tfConsultaKeyPressed

    private void tfNomRuaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfNomRuaKeyPressed
         if(evt.getKeyCode() == 10 ){
            tfCodBai.requestFocusInWindow();
            tfCodBai.selectAll();
          }
    }//GEN-LAST:event_tfNomRuaKeyPressed

    private void btConsultarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btConsultarActionPerformed
         DefaultTableModel modelo = (DefaultTableModel) tbRuas.getModel();
        modelo.setRowCount(0);
        Statement stm = null;
        String search = "select bcod,rcod,rnom,bnom from cparc.cad_vw_bairuas where rnom like \"%" +tfConsulta.getText() + "%\"" ;
        int i=0;

        try  {
            stm = (Statement) conexao.getConnection(true).createStatement();
            ResultSet rs = stm.executeQuery(search);
            rs.afterLast();
            while(rs.previous()){
                modelo.addRow(new String[] {Integer.toString(rs.getInt("rcod")), rs.getString("rnom"),rs.getString("bcod"),rs.getString("bnom") });
                modelo.fireTableCellUpdated(i, 0);
                modelo.fireTableCellUpdated(i, 1);
                modelo.fireTableCellUpdated(i, 2);
                modelo.fireTableCellUpdated(i, 3);
                i++;
            }
        }catch(Exception e){
            String status = e.getMessage();
            System.out.println(status);
        }
    }//GEN-LAST:event_btConsultarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton altRuas;
    private javax.swing.JButton btConsultar;
    private javax.swing.JButton cadRuas;
    private javax.swing.JButton excRuas;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private java.awt.Label label1;
    private java.awt.Label label2;
    private java.awt.Label label3;
    private java.awt.Label label4;
    private javax.swing.JButton salRuas;
    private javax.swing.JTable tbRuas;
    private java.awt.TextField tfCodBai;
    private java.awt.TextField tfCodRua;
    private java.awt.TextField tfConsulta;
    private java.awt.TextField tfNomBai;
    private java.awt.TextField tfNomRua;
    // End of variables declaration//GEN-END:variables

}
